<?php

include_once($_SERVER['DOCUMENT_ROOT'] . "/RecJP/inc/session_check.php");
include_once($_SERVER['DOCUMENT_ROOT'] . "/RecJP/class/class.QuickTest.php");
$QuickTest = new QuickTest();
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    header('Content-type: application/json');
    parse_str($_SERVER['QUERY_STRING']);
    if ($v == "set") {
        try {
            $wordid = $_POST['wordid'];
            $right = false;
            if ($_POST['right'] == 1) {
                $right = true;
            }
            $QuickTest->SetOptions($wordid, $right);
            $arr["returnval"] = $right;
            echo json_encode($arr); //输出json数据 
        } catch (Exception $e) {
            //$arr = array();
            $arr["returnval"] = -1;
            echo json_encode($arr); //输出json数据 
        }
    } else if ($v == "getNext") {
        $wid = $QuickTest->GetNextFromListFromSession();
        if ($wid <= 0) {
            $arr["finished"] = true;
            echo json_encode($arr);
            die();
        }

        $quizType = isset($_SESSION["quizType"]) ? $_SESSION["quizType"] : 0;
        if ($quizType == 1) {
            $template = 1;
        } elseif ($quizType == 2) {
            $template = 2;
        } else {
            $template = mt_rand(1, 2);
        }

        $arr = array();
        $arr["template"] = $template;
        if ($template == 1) {
            $correctItem = null; //正确项
            $Options = $QuickTest->GetOptions($wid, $correctItem);
            $str = "";
            $i = 0;
            foreach ($Options as $key => $value) {
                $i++;
                $str.="<div name=\"sel\" class=\"div300 offset10 padding_topbottomleft5 marginbottom5 fontsize1 overflow_visible cursor_pointer\"";
                $str.=" tag=\"" . $value["right"] . "\">";
                //{$smarty.foreach.name.iteration}).&nbsp;{$arrayitem.read}&nbsp;&nbsp;{$arrayitem.species}.{$arrayitem.meaning} 
                $str.="$i).&nbsp" . $value["read"] . "&nbsp;&nbsp;" . $value["species"] . $value["meaning"];
                $str.="</div>";
            }
            $arr["options"] = $str;
            $arr["word"] = $correctItem["word"];
            $arr["wordid"] = $correctItem["wordid"];
            echo json_encode($arr);
        } else if ($template == 2) {
            $result = $QuickTest->GetWordBywid($wid);
            if (count($result) > 0) {
                $arr["word"] = $result[0]["word"];
                $arr["species"] = $result[0]["species"];
                $arr["wordid"] = $result[0]["wordid"];
                $arr["meaning"] = $result[0]["meaning"];
            }
            echo json_encode($arr);
        }
    } else if ($v == "skip") {
        $wordid = $_POST['wordid'];
        $QuickTest->RemoveOneElementFromSession($wordid);
    } else if ($v == "getTestResult") {
        $sum = 0;
        $rightcount = 0;
        if (isset($_COOKIE['TestedAndWrongList'])) {
            foreach ($_COOKIE['TestedAndWrongList'] as $key => $value) {
                $sum+=$value;
                //$errorcount+=$value;
            }
        }

        if (isset($_COOKIE['TestedAndRightList'])) {
            foreach ($_COOKIE['TestedAndRightList'] as $key => $value) {
                SetCookie("TestedAndRightList[$key]", NULL);
                $sum+=$value;
                $rightcount+=$value;
            }
        }
        $arr = array();
        $arr["sum"] = $sum;
        $arr["rightcount"] = $rightcount;
        $arr["rate"] = $sum == 0 ? 0 : round($rightcount / $sum, 4) * 100;
        echo json_encode($arr);
    }
}